package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.ArrayList;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutSingleBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.updatePatientInfo.UpdatePutNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.DoorPutScanViewModel;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sinyo on 2017/8/14.
 */

public class DoorPutScanPresenter extends BasePresenter<DoorPutScanViewModel> {
    ArrayList<PutSingleBeen> FitstTotal;
    private ApiService mApiService;
    private MDialog waitDialog;
    public String DJJLID;

    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
        receiveData();
    }

    public void receiveData() {
        FitstTotal= (ArrayList<PutSingleBeen>) activity.getIntent().getSerializableExtra("putSingle");
        DJJLID = activity.getIntent().getExtras().getString("DJJLID");
//        Log.e("PXY", "DoorPutScanPresenter: " + FitstTotal.toString() +"    FitstTotal."+FitstTotal.size());
        if (FitstTotal != null) {
            viewModel.show(FitstTotal);
        }
    }
    //TODO:更新药单  上架已完成  PDAU05
//    DJJLID	处方号	Varchar2(20)
//    DJDQZT	单据当前状态 	Number(1)
//    CZYHID	操作用户ID	Varchar2(20)
    public  void  update(String DJDQZT,String CZYHID){
        Disposable disposable = null;
        boolean result;
        disposable = mApiService.doXml(XmlUtils.prepare(UpdatePutNode.getContent("PDAU05",DJJLID,"120101",DJDQZT,CZYHID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {

                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        if ("-1".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitFail("该单据已抓完药更新过状态, 请核对！");
                        } else if ("0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            mOnSubmitResult.submitSuccess();
                        }else {
                            mOnSubmitResult.submitFail("服务器异常");
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    //药单更新监听
    private OnSubmitHospitalResult mOnSubmitResult;

    public void registeOnSubmitResultListener(OnSubmitHospitalResult mOnScanResult) {
        this.mOnSubmitResult = mOnScanResult;
    }

    public void unRegisteOnSubmitResultListener() {
        this.mOnSubmitResult = null;
    }
    //药单更新监听
    public interface OnSubmitHospitalResult {
        void submitSuccess();
        void submitFail(String errMsg);
    }
}
